import subprocess
import IPy

def is_ip(address):
    try:
        global ips
        ips = IPy.IP(address)
        return True
    except:
        print('宁的输出有问题，请再次输入。')
        return False

ip_user = input("请输入要检查的网段，子网掩码：")
while (is_ip(ip_user) == False) :
    ip_user=input("请再次输入：")

#ips = IPy.IP(ip_user)
#ips=['192.168.0.0','192.168.0.1','192.168.0.2']

fp=open("Ip存活记录.txt",'a+')

for i in ips:
    p=subprocess.Popen("ping -c 3 {0} \n".format(i),stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True )
    out,err = p.communicate()
    if p.returncode == 0:
        print(f"{i}通")
        fp.write(f"{i}已经被使用过了\n")
    else:
        print(f"{i}不通")
        fp.write(f"{i}未被使用过\n")

fp.close()

